package org.example.springbootdemo;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @author: Themberfue
 * @date: 2024/11/26 14:03
 * @description:
 */
@RequestMapping("/request")
@RestController
public class RequestController {
    @RequestMapping("/r1")
    public String r1(String keyword) {
        return "接收参数：" + keyword;
    }

    @RequestMapping("/r2")
    public String r2(String username, String password) {
        return "username：" + username + ", password：" + password;
    }

    @RequestMapping("/r3")
    public String r3(Integer count) {
        return "count：" + count;
    }

    @RequestMapping("/r4")
    public String r4(int count) {
        return "count：" + count;
    }

    @RequestMapping("/r5")
    public String r5(UserInfo userInfo) {
        return "userInfo：" + userInfo;
    }

    // 用户端传来的参数名为 q，但将其转化为了 keyword 进行使用
    @RequestMapping("/r6")
    public String r6(@RequestParam("q") String keyword) {
        return "keyword：" + keyword;
    }
    // 非必须传参数名为 q，但接收值为 null
    @RequestMapping("/r6_1")
    public String r6_1(@RequestParam(value = "q", required = false) String keyword) {
        return "keyword：" + keyword;
    }

    // 传递数组
    @RequestMapping("/r7")
    public String r7(String[] arr) {
        return "arr：" + Arrays.toString(arr);
    }

    // 传递集合
    @RequestMapping("/r8")
    public String r8(@RequestParam List<Integer> list) {
        return "list：" + list;
    }

    // 传递JSON
    @RequestMapping("/r9")
    public String r9(@RequestBody UserInfo userInfo) {
        return userInfo.toString();
    }

    // 从URL中获取参数
    @RequestMapping("/article/{articleId}")
    public String r10(@PathVariable Integer articleId) {
        return "articleId:" + articleId;
    }
    // 从URL中获取多个参数
    @RequestMapping("/article/{type}/{articleId}")
    public String r11(@PathVariable String type, @PathVariable Integer articleId) {
        return "type: " + type + ", articleId: " + articleId;
    }

    // 上传文件
    @RequestMapping("/r12")
    public String r12(@RequestPart("file1") MultipartFile file) throws IOException {
        file.transferTo(new File("D:\\UserData\\Downloads\\" + file.getOriginalFilename()));
        return "文件上传成功:" + file.getOriginalFilename();
    }

    // 通过 HttpServletRequest 获得 Cookie
    @RequestMapping("/r13")
    public String r13(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null) {
            System.out.println("Cookie为空");
        } else {
            for (Cookie cookie: cookies) {
                System.out.println(cookie.getName() + ": " + cookie.getValue());
            }
        }
        return "获得Cookie成功";
    }

    // 通过注解直接获取Cookie
    @RequestMapping("/r14") // * 直接通过注解获取Cookie
    public String r14(@CookieValue("test") String test) {
        return "从Cookie中获得test的值" + test;
    }

    // 设置一个session
    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request) {
        // 从cookie中获取sessionId，根据sessionId获取session对象
        // * 若不存在sessionId，则创建一个空的session对象
        HttpSession session = request.getSession();
        // session对象默认存储在内存中
        session.setAttribute("name", "张三");
        session.setAttribute("age", 18);
        return "设置session成功";
    }

    // 获取session
    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request) {
        // 从cookie中获取sessionId，根据sessionId获取session对象
        // * 若不存在sessionId，则返回null
        HttpSession session = request.getSession(false);
        if (session == null) {
            return "用户未登录";
        } else {
            return "用户名：" + session.getAttribute("name");
        }
    }

    // 通过参数直接获取session
    @RequestMapping("/getSession2")
    public String getSession2(HttpSession session) { // * 直接在参数里写好HttpSession对象
        return "用户名：" + session.getAttribute("name");
    }

    // 通过注解直接获取session
    @RequestMapping("/getSession3")
    public String getSession3(@SessionAttribute("name") String name) {
        return "用户名：" + name;
    }

    // 获取请求头任意键值对
    @RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        return "User-Agent = " + userAgent;
    }

    // 通过注解直接获取请求头任意键值对
    @RequestMapping("/getHeader2")
    public String getHeader2(@RequestHeader("User-Agent") String userAgent) {
        return "User-Agent = " + userAgent;
    }
}
